#include <string.h>
#include "gkt_driver.h"
#include "gkt_door.h"
#include "gkt_backboard.h"
#include "window_common.h"
#if defined(GKT_CONFIG_FACE_ID_ENABLE) && GKT_CONFIG_FACE_ID_ENABLE
#include "gkt_face_id.h"
#endif

#if defined(GKT_CONFIG_DOOR_LOCK_AUTOMATIC) && GKT_CONFIG_DOOR_LOCK_AUTOMATIC

enum {
	TIMER_ID_TIMEOUT,
	TIMER_ID_EXIT,
	TIMER_ID_MAX
};

enum {
	WIDGET_ID_HINT,
	WIDGET_ID_MAX
};
#define WINDOW_KEY_OPEN_LOCK_TIMEOUT_TIME_MS 6000
#define WINDOW_KEY_CLOSE_LOCK_STANDBY_TIME_MS 2000

static void window_key_open_lock_done(uint32_t voice_id)
{
	gkt_window_voice_play(NULL, voice_id, GKT_WINDOW_VOICE_F_NOTIFY_END, NULL);
}
static void window_key_open_lock_opened(void)
{
	window_key_open_lock_done(WINDOW_VOICE_COMBINED_ID(lock_already_opened));
}
static void window_key_open_lock_closed(void)
{
	gkt_window_timer_clear(TIMER_ID_TIMEOUT);
	window_key_open_lock_done(WINDOW_VOICE_COMBINED_ID(lock_already_closed));
	gkt_window_timer_set(TIMER_ID_EXIT, WINDOW_KEY_CLOSE_LOCK_STANDBY_TIME_MS, 0, 0);
}
static void window_key_open_lock_failed(void)
{
	gkt_window_timer_clear(TIMER_ID_TIMEOUT);
	window_key_open_lock_done(WINDOW_VOICE_COMBINED_ID(operation_timeout));
	gkt_window_timer_set(TIMER_ID_EXIT, WINDOW_KEY_CLOSE_LOCK_STANDBY_TIME_MS, 0, 0);
}

static int window_key_open_lock_event_handler(
				uint32_t major_type, uint32_t minor_type, 
				uint32_t id, uint32_t data)
{
	int retval = GKT_EVENT_HANDLE_COMPLETE;
	uint32_t auto_close_time = (gp_window_data->lock_auto_close_time+2)*1000;

	gkt_info("window_key_open: (%u) (%u- %u)(0x%x - 0x%x)\n",
		auto_close_time, major_type, minor_type, id, data);

	switch(major_type) {
		case GKT_EVENT_WINDOW:
			if (minor_type == GKT_WINDOW_ACTIVE) {
#if defined(GKT_CONFIG_DOOR_TAMPER_ENABLE) && GKT_CONFIG_DOOR_TAMPER_ENABLE
				window_door_tamper_stop();
#endif
				window_common_mask_all();
#if defined(GKT_CONFIG_FACE_ID_ENABLE) && GKT_CONFIG_FACE_ID_ENABLE
				gkt_face_id_send_command(MID_RESET,NULL,0);
				//gkt_screen_control(GKT_SCREEN_CTRL_DISPLAY_OFF,1);
				gkt_keypad_peak_control(GKT_KEYPAD_CTRL_ENABLE);
				gkt_key_led_control(GKT_KEY_LED_CTRL_ENABLE, 0);
				g_window_ram_data.face_id_verify_suc_state = WINDOW_DATA_FACE_ID_STATE_SET;
				g_window_ram_data.silent_mode = 1;
#endif
				window_key_open_lock_opened();
				gkt_window_timer_set(TIMER_ID_TIMEOUT, auto_close_time, 0, 0);
			}
			else if (minor_type == GKT_WINDOW_UNINIT) {
#if defined(GKT_CONFIG_FACE_ID_ENABLE) && GKT_CONFIG_FACE_ID_ENABLE

#else
				gkt_window_system_standby(0);
#endif
			}
			break;
		case GKT_EVENT_SYSTEM:
			if ((GKT_WINDOW_SYSTEM_DOOR == minor_type)
				&& (GKT_DOOR_NOTIFY_LOCK_STATUS == id))
			{
				if (GKT_DOOR_LOCK_ST_DL_CLOSED == data ||
					GKT_DOOR_LOCK_ST_CLOSED == data)
				{
					window_key_open_lock_closed();
				}
				else if (GKT_DOOR_LOCK_ST_DL_CLOSE_FAILED == data ||
						GKT_DOOR_LOCK_ST_CLOSE_FAILED == data)
				{
					window_key_open_lock_failed();
				}
			}
			break;
		case GKT_EVENT_TIMER:
			if (TIMER_ID_TIMEOUT == minor_type)
				window_key_open_lock_failed();
			else if (TIMER_ID_EXIT == minor_type)
				gkt_window_exit();
			break;
		default:
			break;
	}

	return retval;
}
GKT_WINDOW_DESC_DEFINE_NO_WIDGETS(key_open_lock);

#endif

